// main.qml - 时钟应用（纯C++驱动版本）
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Dialogs 1.3
import QtQuick.Controls 2.15

Window {
    width: 800
    height: 1200
    visible: true
    title: "Qt 时钟 for 鸿蒙"

    // 渐变背景
    Rectangle {
        anchors.fill: parent
        gradient: Gradient {
            GradientStop { position: 0.0; color: "#0F2027" }
            GradientStop { position: 0.5; color: "#203A43" }
            GradientStop { position: 1.0; color: "#2C5364" }
        }

        Column {
            anchors.fill: parent
            anchors.topMargin: 40
            anchors.bottomMargin: 40
            anchors.leftMargin: 20
            anchors.rightMargin: 20
            spacing: 40

            // 时钟区域
            Column {
                width: parent.width
                spacing: 40

                // 数字时钟显示 - 直接绑定到C++属性
                Text {
                    id: timeDisplay
                    text: clockModel.currentTime  // 直接绑定到C++ ClockModel
                    font.pixelSize: 120
                    font.bold: true
                    font.family: "Arial"
                    color: "#FFFFFF"
                    anchors.horizontalCenter: parent.horizontalCenter
                    
                    // 发光效果
                    style: Text.Outline
                    styleColor: "#00D4FF"
                }

                // 日期显示
                Column {
                    spacing: 10
                    anchors.horizontalCenter: parent.horizontalCenter

                    Text {
                        id: dateDisplay
                        text: clockModel.currentDate  // 直接绑定到C++ ClockModel
                        font.pixelSize: 36
                        color: "#B0E0E6"
                        anchors.horizontalCenter: parent.horizontalCenter
                    }

                    Text {
                        id: weekDisplay
                        text: clockModel.currentWeekDay  // 直接绑定到C++ ClockModel
                        font.pixelSize: 28
                        color: "#87CEEB"
                        anchors.horizontalCenter: parent.horizontalCenter
                    }
                }

                // 时间格式提示
                Text {
                    text: clockModel.is24HourFormat ? "24小时制 (点击切换)" : "12小时制 (点击切换)"
                    font.pixelSize: 18
                    color: "#87CEEB"
                    opacity: 0.8
                    anchors.horizontalCenter: parent.horizontalCenter
                    
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            clockModel.toggleTimeFormat()  // 调用C++方法切换时间格式
                        }
                    }
                }

                // 装饰性分隔线
                Rectangle {
                    width: 300
                    height: 2
                    color: "#00D4FF"
                    radius: 1
                    anchors.horizontalCenter: parent.horizontalCenter
                    opacity: 0.6
                }

                // 品牌信息
                Text {
                    text: "坚果派 × Qt for 鸿蒙"
                    font.pixelSize: 24
                    color: "#FFFFFF"
                    opacity: 0.7
                    anchors.horizontalCenter: parent.horizontalCenter
                }

                // C++ 驱动说明
                Text {
                    text: "纯C++驱动 ✨"
                    font.pixelSize: 20
                    color: "#00D4FF"
                    opacity: 0.6
                    anchors.horizontalCenter: parent.horizontalCenter
                }
            }

            // 文件选择区域
            Rectangle {
                width: parent.width
                height: fileInfoColumn.height + 40
                color: "#1A2A3A"
                radius: 10
                border.color: "#00D4FF"
                border.width: 2
                opacity: 0.9

                Column {
                    id: fileInfoColumn
                    anchors.centerIn: parent
                    width: parent.width - 40
                    spacing: 15

                    // 标题
                    Text {
                        text: "📁 文件选择器"
                        font.pixelSize: 24
                        font.bold: true
                        color: "#00D4FF"
                        anchors.horizontalCenter: parent.horizontalCenter
                    }

                    // 文件操作按钮组
                    Row {
                        width: parent.width
                        height: 50
                        spacing: 10

                        // 选择文件按钮
                        Rectangle {
                            width: (parent.width - 20) / 3
                            height: parent.height
                            color: "#00D4FF"
                            radius: 5

                            Text {
                                text: "📄 选择文件"
                                font.pixelSize: 16
                                font.bold: true
                                color: "#FFFFFF"
                                anchors.centerIn: parent
                            }

                            MouseArea {
                                anchors.fill: parent
                                onClicked: fileDialog.open()
                            }
                        }

                        // 选择文件夹按钮
                        Rectangle {
                            width: (parent.width - 20) / 3
                            height: parent.height
                            color: "#4CAF50"
                            radius: 5

                            Text {
                                text: "📁 选择文件夹"
                                font.pixelSize: 16
                                font.bold: true
                                color: "#FFFFFF"
                                anchors.centerIn: parent
                            }

                            MouseArea {
                                anchors.fill: parent
                                onClicked: folderDialog.open()
                            }
                        }

                        // 保存文件按钮
                        Rectangle {
                            width: (parent.width - 20) / 3
                            height: parent.height
                            color: "#FF9800"
                            radius: 5

                            Text {
                                text: "💾 保存文件"
                                font.pixelSize: 16
                                font.bold: true
                                color: "#FFFFFF"
                                anchors.centerIn: parent
                            }

                            MouseArea {
                                anchors.fill: parent
                                onClicked: saveDialog.open()
                            }
                        }
                    }

                    // 文件信息显示区域
                    Rectangle {
                        width: parent.width
                        height: fileInfoText.height + 20
                        color: "#0F2027"
                        radius: 5
                        visible: filePicker.hasSelection
                        anchors.horizontalCenter: parent.horizontalCenter

                        Column {
                            id: fileInfoText
                            anchors.centerIn: parent
                            width: parent.width - 20
                            spacing: 8

                            Text {
                                text: "📄 " + filePicker.selectedFileName
                                font.pixelSize: 18
                                color: "#FFFFFF"
                                width: parent.width
                                elide: Text.ElideMiddle
                            }

                            Text {
                                text: "📍 " + filePicker.selectedFilePath
                                font.pixelSize: 14
                                color: "#B0E0E6"
                                width: parent.width
                                elide: Text.ElideMiddle
                            }

                            Row {
                                spacing: 15
                                anchors.horizontalCenter: parent.horizontalCenter

                                Text {
                                    text: filePicker.getFileIcon(filePicker.selectedFilePath) + " " + filePicker.fileType
                                    font.pixelSize: 14
                                    color: "#87CEEB"
                                }

                                Text {
                                    text: "大小: " + filePicker.fileSize
                                    font.pixelSize: 14
                                    color: "#87CEEB"
                                }

                                Text {
                                    text: "修改: " + filePicker.fileModifiedTime
                                    font.pixelSize: 12
                                    color: "#87CEEB"
                                    opacity: 0.8
                                }
                            }
                            
                            // 文件操作按钮
                            Row {
                                spacing: 10
                                anchors.horizontalCenter: parent.horizontalCenter
                                topPadding: 10

                                Rectangle {
                                    width: 80
                                    height: 30
                                    color: "#4CAF50"
                                    radius: 5

                                    Text {
                                        text: "复制"
                                        font.pixelSize: 12
                                        color: "#FFFFFF"
                                        anchors.centerIn: parent
                                    }

                                    MouseArea {
                                        anchors.fill: parent
                                        onClicked: {
                                            var destPath = filePicker.selectedFilePath + ".copy"
                                            filePicker.copyFile(filePicker.selectedFilePath, destPath)
                                        }
                                    }
                                }

                                Rectangle {
                                    width: 80
                                    height: 30
                                    color: "#FF6B6B"
                                    radius: 5

                                    Text {
                                        text: "删除"
                                        font.pixelSize: 12
                                        color: "#FFFFFF"
                                        anchors.centerIn: parent
                                    }

                                    MouseArea {
                                        anchors.fill: parent
                                        onClicked: {
                                            filePicker.deleteFile(filePicker.selectedFilePath)
                                            filePicker.clearSelection()
                                        }
                                    }
                                }

                                Rectangle {
                                    width: 80
                                    height: 30
                                    color: "#2196F3"
                                    radius: 5

                                    Text {
                                        text: "预览"
                                        font.pixelSize: 12
                                        color: "#FFFFFF"
                                        anchors.centerIn: parent
                                    }

                                    MouseArea {
                                        anchors.fill: parent
                                        onClicked: {
                                            var content = filePicker.readTextFile(filePicker.selectedFilePath, 10240)
                                            if (content) {
                                                previewText.text = content
                                                previewDialog.visible = true
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // 清除选择按钮
                    Rectangle {
                        width: parent.width
                        height: 40
                        color: "#FF6B6B"
                        radius: 5
                        visible: filePicker.hasSelection
                        anchors.horizontalCenter: parent.horizontalCenter

                        Text {
                            text: "清除选择"
                            font.pixelSize: 16
                            color: "#FFFFFF"
                            anchors.centerIn: parent
                        }

                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                filePicker.clearSelection()
                            }
                        }
                    }

                    // 提示文本
                    Text {
                        text: filePicker.hasSelection ? "" : "点击上方按钮选择文件"
                        font.pixelSize: 14
                        color: "#87CEEB"
                        opacity: 0.6
                        anchors.horizontalCenter: parent.horizontalCenter
                        visible: !filePicker.hasSelection
                    }
                }
            }

            // 最近文件列表
            Rectangle {
                width: parent.width
                height: recentFilesColumn.height + 40
                color: "#1A2A3A"
                radius: 10
                border.color: "#00D4FF"
                border.width: 2
                opacity: 0.9
                visible: filePicker.recentFiles.length > 0

                Column {
                    id: recentFilesColumn
                    anchors.centerIn: parent
                    width: parent.width - 40
                    spacing: 10

                    Text {
                        text: "📋 最近文件"
                        font.pixelSize: 20
                        font.bold: true
                        color: "#00D4FF"
                        anchors.horizontalCenter: parent.horizontalCenter
                    }

                    Repeater {
                        model: filePicker.recentFiles
                        
                        Rectangle {
                            width: parent.width
                            height: 40
                            color: "#0F2027"
                            radius: 5

                            Row {
                                anchors.left: parent.left
                                anchors.leftMargin: 10
                                anchors.verticalCenter: parent.verticalCenter
                                spacing: 10
                                width: parent.width - 80

                                Text {
                                    text: filePicker.getFileIcon(modelData) + " " + modelData.split("/").pop()
                                    font.pixelSize: 14
                                    color: "#FFFFFF"
                                    elide: Text.ElideMiddle
                                    width: parent.width - 20
                                }
                            }

                            MouseArea {
                                anchors.fill: parent
                                onClicked: {
                                    filePicker.setSelectedFile(modelData)
                                }
                            }

                            Rectangle {
                                anchors.right: parent.right
                                anchors.rightMargin: 10
                                anchors.verticalCenter: parent.verticalCenter
                                width: 30
                                height: 30
                                color: "#FF6B6B"
                                radius: 5

                                Text {
                                    text: "×"
                                    font.pixelSize: 20
                                    color: "#FFFFFF"
                                    anchors.centerIn: parent
                                }

                                MouseArea {
                                    anchors.fill: parent
                                    onClicked: {
                                        filePicker.removeRecentFile(modelData)
                                    }
                                }
                            }
                        }
                    }

                    Rectangle {
                        width: parent.width
                        height: 30
                        color: "#FF6B6B"
                        radius: 5

                        Text {
                            text: "清空最近文件"
                            font.pixelSize: 14
                            color: "#FFFFFF"
                            anchors.centerIn: parent
                        }

                        MouseArea {
                            anchors.fill: parent
                            onClicked: filePicker.clearRecentFiles()
                        }
                    }
                }
            }

            // 监听文件选择信号
            Connections {
                target: filePicker
                
                onFileSelected: function(filePath) {
                    console.log("文件已选择:", filePath)
                }
                
                onErrorOccurred: function(error) {
                    console.error("文件选择错误:", error)
                }
            }
        }

        // 工具函数：处理文件路径
        function getLocalPath(fileUrl) {
            var filePath = fileUrl.toString()
            if (filePath.startsWith("file://")) {
                filePath = filePath.substring(7)
            }
            return decodeURIComponent(filePath)
        }

        // 文件选择对话框
        FileDialog {
            id: fileDialog
            title: "选择文件"
            folder: shortcuts.home
            nameFilters: [
                "图片文件 (*.png *.jpg *.jpeg *.gif *.bmp)",
                "文本文件 (*.txt *.md *.log)",
                "所有文件 (*.*)"
            ]
            
            onAccepted: {
                var filePath = getLocalPath(fileDialog.fileUrl)
                filePicker.setSelectedFile(filePath)
            }
        }

        // 文件夹选择对话框
        FileDialog {
            id: folderDialog
            title: "选择文件夹"
            folder: shortcuts.home
            selectFolder: true
            
            onAccepted: {
                var dirPath = getLocalPath(folderDialog.fileUrl)
                filePicker.setSelectedDirectory(dirPath)
            }
        }

        // 文件保存对话框
        FileDialog {
            id: saveDialog
            title: "保存文件"
            folder: shortcuts.documents
            selectExisting: false
            
            onAccepted: {
                var filePath = getLocalPath(saveDialog.fileUrl)
                // 创建新文件
                var content = "这是新创建的文件\n创建时间: " + new Date().toLocaleString()
                if (filePicker.writeTextFile(filePath, content)) {
                    filePicker.setSelectedFile(filePath)
                }
            }
        }

        // 文件预览对话框
        Rectangle {
            id: previewDialog
            anchors.fill: parent
            color: "#80000000"
            visible: false

            Rectangle {
                anchors.centerIn: parent
                width: parent.width * 0.8
                height: parent.height * 0.6
                color: "#1A2A3A"
                radius: 10
                border.color: "#00D4FF"
                border.width: 2

                Column {
                    anchors.fill: parent
                    anchors.margins: 20
                    spacing: 10

                    Row {
                        width: parent.width
                        height: 40

                        Text {
                            text: "📄 文件预览"
                            font.pixelSize: 20
                            font.bold: true
                            color: "#00D4FF"
                        }

                        Item { width: parent.width - 200; height: 1 }

                        Rectangle {
                            width: 60
                            height: 30
                            color: "#FF6B6B"
                            radius: 5

                            Text {
                                text: "关闭"
                                font.pixelSize: 14
                                color: "#FFFFFF"
                                anchors.centerIn: parent
                            }

                            MouseArea {
                                anchors.fill: parent
                                onClicked: previewDialog.visible = false
                            }
                        }
                    }

                    Rectangle {
                        width: parent.width
                        height: parent.height - 50
                        color: "#0F2027"
                        radius: 5

                        Flickable {
                            anchors.fill: parent
                            anchors.margins: 10
                            contentWidth: previewText.width
                            contentHeight: previewText.height
                            clip: true

                            Text {
                                id: previewText
                                width: parent.width
                                font.pixelSize: 14
                                color: "#FFFFFF"
                                wrapMode: Text.Wrap
                                text: ""
                            }
                        }
                    }
                }
            }

            MouseArea {
                anchors.fill: parent
                onClicked: previewDialog.visible = false
            }
        }

        // 注意：不再需要Timer和JavaScript代码
        // 所有逻辑都由C++ ClockModel处理
        // QML只负责显示和用户交互
    }
}